package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 拦截器配置类
 */
@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    // 注册拦截器，设置拦截路径
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/employee/login") // 排除匹配到该请求路径的请求
                .excludePathPatterns("/backend/**") // 排除匹配到该请求路径的请求
                .excludePathPatterns("/front/**") // 排除匹配到该请求路径的请求
                .excludePathPatterns("/user/login") // 排除用户登录
                .excludePathPatterns("/user/sendMsg"); // 排除用户登录时发送短信
    }

    // 扩展MVC消息转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 设置自定义核心对象JacksonObjectMapper
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        // 添加消息转换器至集合开头，提升使用优先级
        converters.add(0, messageConverter);
//        log.info("{}", converters);
    }
}
